package com.baosight.sczx.zm.cm.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.cInterface.Department;
import com.baosight.sczx.zm.cm.domain.ZMCMExportApplication;
import com.baosight.sczx.zm.cm.domain.ZMCMExportApplicationDetail;


/**领料申请管理
 * <p>Title: ServiceZMCM80.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 张坤
 * @version 1.0
 * <pre>History:
 *          2011-06-21 上午09:38:09  张坤  Create
 *</pre>
 */
public class ServiceZMCM80 extends ServiceEPBase {
	
	/**
	 * 初始化数据
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCM80</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-06-21下午08:44:45 张坤
	 * </p>
	 *
	 */
	public EiInfo initLoad(EiInfo inInfo) 
	{
		EiInfo outInfo;
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		outInfo = this.query(inInfo);
		outInfo.addBlock(Department.getInstance().getConstructionUnit("cmes.cm.department", "orgCode", "orgName", false));
		return outInfo;
	}
	
	/**
	 * 主表数据查询方法
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCM80</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-06-21 上午09:38:09  张坤  Create
	 * </p>
	 *
	 */
	@Override
	public EiInfo query(EiInfo inInfo) 
	{
		if(inInfo.getBlock(EiConstant.queryBlock) == null)
		{
			inInfo.addBlock(EiConstant.queryBlock);
		}
		inInfo.getBlock(EiConstant.queryBlock)
			.setCell(0, "fdApplicationDepartment", UserSession.getInSessionProperty("orgCode"));
		EiInfo outInfo = super.query(inInfo, "ZMCMExportApplication.query", new ZMCMExportApplication());
		return outInfo;
	}
	
	/**
	 * 	上报数据
	 * <p>Function: Approve</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2010-12-25下午04:51:27 张坤
	 * </p>
	 *
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public EiInfo Approve(EiInfo inInfo) 
	{
		for(int i=0; i<inInfo.getBlock(EiConstant.resultBlock).getRowCount(); i++)
		{
			String fdKeyId = inInfo.getBlock(EiConstant.resultBlock).getCellStr(i, "fdKeyId");
			Map inMap = new HashMap();
			inMap.put("fdApplicationId", fdKeyId);
			List _list = dao.query("ZMCMExportApplicationDetail.queryStrict", inMap);
			
			if(_list.size() == 0)
			{
				inInfo.setStatus(-1);
				inInfo.setMsg("上报的申请中包含无明细的领料单，请重新选择！");
				return inInfo;
			}
		}
		
		EiInfo _temp = super.update(inInfo, "ZMCMExportApplication.report");
        EiInfo outInfo = this.query(inInfo);
        outInfo.setMsg(_temp.getMsg());
        outInfo.setDetailMsg(_temp.getDetailMsg());
       
		return outInfo;
	}
	
	/**
	 * 
	 * <p>Function: updateTeamPlan</p>
	 * <p>Description: </p>
	 * <p>return value:void</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-12-2上午03:23:25 严洪浩
	 * </p>
	 *
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	private void updateTeamPlan(EiInfo inInfo)
	{
		String fdKeyId = inInfo.getBlock(EiConstant.resultBlock).getCellStr(0, "fdKeyId");
		Map inMap = new HashMap();
		inMap.put("fdApplicationId", fdKeyId);
		List _list = dao.query("ZMCMExportApplicationDetail.queryStrict", inMap);
		
		for(int i=0; i<_list.size(); i++)
		{
			ZMCMExportApplicationDetail md = (ZMCMExportApplicationDetail)_list.get(i);
			inMap = new HashMap();
			inMap.put("fdApplicationNumber", 0-md.getFdApplicationNumber());
			inMap.put("fdRequirePlan", md.getFdRequirePlan());
					
			dao.update("ZMCMTeammaterialplanDetail.updatefdApplicationExportNumForNoCode", inMap);
			
			inMap.put("fdStatus", "部分领用");
			dao.update("ZMCMTeammaterialplanDetail.updatefdStatusForNoCode", inMap);
		}
	}

	/**
	 * 主表数据删除方法
	 * <p>Function: delete</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCM80</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-06-17 上午09:38:09  张坤  Create
	 * </p>
	 *
	 */
	@Override
	public EiInfo delete(EiInfo inInfo) 
	{
		updateTeamPlan(inInfo);
		super.delete(inInfo, "ZMCMExportApplicationDetail.super_delete");
		EiInfo _temp = super.delete(inInfo, "ZMCMExportApplication.delete");
		
        EiInfo outInfo = this.query(inInfo);
        outInfo.setMsg(_temp.getMsg());
        outInfo.setDetailMsg(_temp.getDetailMsg());
        
		return outInfo;
	}
		
	/**
	 * 校验序号不能重复
	 * <p>Function: validate</p>
	 * <p>Description: </p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-06-17 上午09:38:09  张坤  Create
	 * </p>
	 *
	 */
	@SuppressWarnings({ "rawtypes", "unused" })
	private boolean validate(EiInfo inInfo, String sqlName, Map<String, String> params) {
		if(params == null) {
			params = new HashMap<String, String>();
		}
		params.put("fdApplicationCode", inInfo.getBlock("result").getCellStr(0, "fdApplicationCode"));
		List result = dao.query("ZMCMExportApplication.validate", params);
		if(result!=null && result.size() > 0) {
			inInfo.setStatus(-1);
			inInfo.setMsg("领料单号不能重复！");
			return true;
		}
		return false;
	}
	
	/**
	 * 根据出库来更改状态
	 * <p>Function: updateForStatusToPart</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-7-15上午10:07:49 余虎
	 * </p>
	 *
	 */
	public EiInfo updateForStatus(EiInfo inInfo) {
		Map<String, String> map = new HashMap<String, String>();
		map.put("fdKeyId", inInfo.getString("fdKeyId"));
		map.put("fdStatus", inInfo.getString("fdStatus"));
		dao.update("ZMCMExportApplication.updateForStatus", map);
		return inInfo;
	}
}
